// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// u [author] rhyme_lph
// A [email]  rhymelph@gmail.com
// b [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/dashboard_drawer_provider.dart';
import 'package:finance_app/src/provider/dashboard_provider.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:url_launcher/url_launcher.dart';

class DashboardDrawerView extends StatefulWidget {
  @override
  _DashboardDrawerViewState createState() => _DashboardDrawerViewState();
}

class _DashboardDrawerViewState extends State<DashboardDrawerView> with StatefulPresenter<DashboardDrawerView, DashboardProvider, DashboardDrawerProvider> {
  @override
  Widget buildWidget(BuildContext context) => Material(
        clipBehavior: Clip.hardEdge,
        type: MaterialType.transparency,
        child: Container(
          width: MediaQuery.of(context).size.width * 3 / 4,
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                topRight: Radius.circular(getDp(20)),
                bottomRight: Radius.circular(getDp(20)),
              )),
          child: Column(
            children: [
              buildDrawerHeader(context),
              Expanded(
                child: CustomScrollView(
                  slivers: [
                    SliverList(
                        delegate: SliverChildListDelegate(provider.drawerList
                            .map((e) => e['isLine'] == true
                                ? _buildLine()
                                : _buildDrawerItem(
                                    title: e['title'],
                                    assets: e['asset'],
                                    router: e['router'],
                                  ))
                            .toList())),
                  ],
                ),
              ),
            ],
          ),
        ),
      );

  SizedBox buildDrawerHeader(BuildContext context) {
    return SizedBox(
      height: MediaQuery.of(context).padding.top + getDp(132),
      child: DrawerHeader(
          padding: EdgeInsets.only(
            left: getDp(33),
            right: getDp(40),
          ),
          decoration: BoxDecoration(
            color: aceWhiteGreyColor,
          ),
          child: PressedOpacityButton(
            onPressed: provider.onNavUserProfile,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                CircleAvatar(
                  radius: getDp(33.5),
                  backgroundImage: Config.toImageProvider(Config.userProfile?.avatar),
                  backgroundColor: aceGrayColor,
                ),
                SizedBox(
                  width: getDp(20),
                ),
                Expanded(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      rootListBuilder<String>(
                        selector: (_, __) => Config.userProfile?.nickname ?? Config.userProfile?.name ?? '',
                        builder: (BuildContext context, String value, Widget child) => Text(
                          value,
                          style: TextStyle(
                            color: aceDarkGrayColor,
                            fontSize: getSp(18),
                            fontWeight: FontWeight.w700,
                            height: 22 / 18,
                          ),
                        ),
                      ),
                      SizedBox(
                        height: getDp(8),
                      ),
                      rootListBuilder<String>(
                        selector: (_, __) => Config.currentSubscription == null || Config.currentSubscription.isTrial == 1
                            ? S.current.free_trial
                            : Config.currentSubscription?.plan?.nameText,
                        builder: (BuildContext context, String value, Widget child) => Container(
                          child: Text('$value',
                              style: TextStyle(
                                color: aceDarkGrayColor,
                                fontSize: getSp(12),
                                fontWeight: FontWeight.w700,
                                height: 1,
                              )),
                          decoration: BoxDecoration(color: aceGrayColor, borderRadius: BorderRadius.circular(getDp(4))),
                          padding: EdgeInsets.symmetric(
                            horizontal: getDp(12),
                            vertical: getDp(4),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                SvgPicture.asset(
                  A.assetsImagesRight,
                  width: getDp(16),
                  height: getDp(16),
                ),
              ],
            ),
          )),
    );
  }

  Widget _buildDrawerItem({String title, String assets, String router}) => listBuilder<bool>(
      builder: (BuildContext context, bool value, Widget child) => PressedOpacityButton(
            onPressed: () => provider.onClick(title, router),
            padding: EdgeInsets.only(
              right: getDp(20),
              top: getDp(12.5),
              bottom: getDp(12.5),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Container(
                  width: getDp(5),
                  height: getDp(28),
                  decoration: BoxDecoration(
                    color: value ? aceBlueColor : Colors.transparent,
                    borderRadius: BorderRadius.circular(getDp(5)),
                  ),
                  margin: EdgeInsets.only(right: getDp(28)),
                ),
                SizedBox(
                  width: getDp(22),
                  height: getDp(22),
                  child: SvgPicture.asset(
                    assets,
                    fit: BoxFit.contain,
                  ),
                ),
                SizedBox(
                  width: getDp(25),
                ),
                Expanded(
                    child: Text(
                  title,
                  style: TextStyle(
                    color: value ? aceBlueColor : aceBlackColor,
                    fontSize: getSp(16),
                    fontWeight: value ? FontWeight.w700 : FontWeight.w400,
                    height: 22 / 16,
                  ),
                ))
              ],
            ),
          ),
      selector: (_, __) => title == __.currentSelectDrawer);

  Widget _buildLine() => Divider(
        height: getDp(8),
      );
}
